import 'dart:async';

import 'package:flutter/services.dart';

const String _KeyBoardEventChannelName = "keyboard_visibility_event";
const int _KeyBoardIsVisible = 1; // 键盘是否可视

///
/// 键盘事件相关
///
class KeyBoardEvent {
  static const _eventChannel = const EventChannel(_KeyBoardEventChannelName);

  static bool _isInitialized = false;

  static final _onChangeController = StreamController<bool>();
  static final _onChange$ = _onChangeController.stream.asBroadcastStream();

  static Stream<bool> get onChange$ {
    if (!_isInitialized) {
      _eventChannel
          .receiveBroadcastStream()
          .map<bool>((e) => e == _KeyBoardIsVisible)
          .listen((data) => _onChangeController.add(data));
      _isInitialized = true;
    }
    return _onChange$;
  }
}
